package com.yaokai.yygh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yaokai.yygh.common.exception.HosptialException;
import com.yaokai.yygh.common.result.ResultCodeEnum;
import com.yaokai.yygh.mapper.HospitalSetMapper;
import com.yaokai.yygh.model.hosp.HospitalSet;
import com.yaokai.yygh.service.HospitalSetService;
import com.yaokai.yygh.vo.order.SignInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper, HospitalSet> implements HospitalSetService {

    @Resource
    private HospitalSetMapper hospitalSetMapper;

    //2 根据传递过来医院编码，查询数据库，查询签名
    @Override
    public String getSignKey(String hoscode) {
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        wrapper.eq("hoscode",hoscode);
        HospitalSet hospitalSet = baseMapper.selectOne(wrapper);
        return hospitalSet.getSignKey();
    }

    @Override
    public SignInfoVo getSignInfoVo(String hoscode) {
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        wrapper.eq("hoscode",hoscode);
        HospitalSet hospitalSet = baseMapper.selectOne(wrapper);
        if(null == hospitalSet) {
            throw new HosptialException(ResultCodeEnum.HOSPITAL_OPEN);
        }
        SignInfoVo signInfoVo = new SignInfoVo();
        signInfoVo.setApiUrl(hospitalSet.getApiUrl());
        signInfoVo.setSignKey(hospitalSet.getSignKey());
        return signInfoVo;
    }

    @Override
    public boolean codeIsExist(String hoscode ,Long id) {
  //      QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
//        wrapper.eq("hoscode",hoscode);
//        int count =  super.baseMapper.selectCount(wrapper);
//        if(count>0) {
//            return true;
//        }
 //       wrapper.eq("is_deleted",-1);
    //    int count=super.baseMapper.selectCount(wrapper);
        return hospitalSetMapper.codeCount(hoscode ,id)>0;
    }


}
